package com.kugong.signUp;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;
import java.util.List;

/**
 * mysql> desc tb_sign_up;
 +-------------+-------------+------+-----+---------+----------------+
 | Field       | Type        | Null | Key | Default | Extra          |
 +-------------+-------------+------+-----+---------+----------------+
 | id          | int(11)     | NO   | PRI | NULL    | auto_increment |
 | name        | varchar(50) | YES  | MUL | NULL    |                |
 | contact     | varchar(50) | YES  |     | NULL    |                |
 | create_time | datetime    | NO   |     | NULL    |                |
 | userId      | varchar(10) | YES  |     | NULL    |                |
 | matchId     | varchar(10) | YES  |     | NULL    |                |
 +-------------+-------------+------+-----+---------+----------------+
 * Created by lenovo on 2015/8/2.
 */
public class SignUp extends Model<SignUp>{
    public final static SignUp dao = new SignUp();

    /**
     * 分页查询报名信息
     * @param page
     * @param size
     * @param matchId
     * @return Page<SignUp>
     */
    public Page<SignUp> paginate(int page,int size,String matchId){
        return paginate(page,size,"select *","from tb_sign_up where matchId = ? order by id",matchId);
    }

    /**
     * 根据战网ID和比赛ID获取报名信息
     * @param signName
     * @param matchId
     * @return
     */
    public List<SignUp> qrySignUpByNameAndMatch(String signName,String matchId){
        return find("select * from tb_sign_up where name = ? and matchId = ?", signName, matchId);
    }

    /**
     * 根据用户ID和比赛ID获取报名信息
     * @param userId
     * @param matchId
     * @return
     */
    public SignUp qrySignUpByUserIdAndMatchId(String userId,String matchId){
        return  findFirst("select * from tb_sign_up where userId = ? and matchId = ?", userId, matchId);
    }

    /**
     * 添加报名信息
     * @param userId
     * @param matchId
     * @param signName
     */
    public void addSignUp(String userId,String matchId,String signName){
        SignUp signUp = new SignUp();
        signUp.set("create_time", new Date()).set("userId", userId).set("matchId",matchId).set("name", signName).save();
    }

    /**
     * 查询生成对战表所需的报名信息
     * @param matchId
     * @param playerNum
     */
    public List<SignUp> qrySignUpForBattle(String matchId,int playerNum){
        return find("select id,contact,create_time,userId,(select signName from tb_user where id = userId) as name,matchId from tb_sign_up where matchId=? order by id limit ?", matchId, playerNum);
    }
}
